Design Pattern is used in .NET Core, why it’s useful, and a tiny real-world example

 

1️⃣ Singleton Pattern

πŸ‘‰ Where we use it in .NET Core

  • Logging

  • Configuration

  • Caching

  • In-memory shared data

πŸ‘‰ Why useful

  • Ensures only one instance exists in the entire app

  • Saves memory

  • Keeps data consistent

πŸ‘‰ Real .NET Core examples

  • ILogger

  • IConfiguration

  • IMemoryCache

πŸ‘‰ Example

builder.Services.AddSingleton<AppSettings>();
public class AppSettings { public string AppName { get; set; } }

πŸ—£ Interview line

“I use Singleton for shared services like configuration, caching, and logging where only one instance is needed.”


2️⃣ Factory Pattern

πŸ‘‰ Where we use it

  • Creating objects based on condition or input

  • Payment gateways

  • Notification services

  • File handlers (PDF, Excel, CSV)

πŸ‘‰ Why useful

  • Centralizes object creation logic

  • Removes if/else from business code

  • Follows Open/Closed Principle

πŸ‘‰ Example (Notification Factory)

public interface INotification { void Send(string msg); } public class EmailNotification : INotification { public void Send(string msg) => Console.WriteLine("Email: " + msg); } public class SmsNotification : INotification { public void Send(string msg) => Console.WriteLine("SMS: " + msg); } public class NotificationFactory { public static INotification Create(string type) { return type switch { "Email" => new EmailNotification(), "SMS" => new SmsNotification(), _ => throw new Exception("Invalid type") }; } }

πŸ—£ Interview line

“Factory pattern helps me decide which object to create at runtime without exposing creation logic.”


3️⃣ Builder Pattern

πŸ‘‰ Where we use it

  • Building complex objects

  • Request/response objects

  • Configuration objects

  • Fluent APIs

πŸ‘‰ Why useful

  • Avoids long constructors

  • Improves readability

  • Step-by-step object creation

πŸ‘‰ Example (API Request Builder)

public class ApiRequest { public string Url { get; set; } public string Method { get; set; } public string Body { get; set; } }
public class ApiRequestBuilder { private ApiRequest _req = new(); public ApiRequestBuilder SetUrl(string url) { _req.Url = url; return this; } public ApiRequestBuilder SetMethod(string method) { _req.Method = method; return this; } public ApiRequestBuilder SetBody(string body) { _req.Body = body; return this; } public ApiRequest Build() => _req; }

πŸ—£ Interview line

“Builder is useful when objects have many optional parameters and need clean construction.”


4️⃣ Adapter Pattern

πŸ‘‰ Where we use it

  • Integrating legacy systems

  • Third-party APIs

  • Old interfaces with new code

πŸ‘‰ Why useful

  • Connects incompatible interfaces

  • No change required in old code

πŸ‘‰ Example (Old payment system)

public class OldPaymentGateway { public void MakePayment(double amount) { Console.WriteLine("Paid using old system"); } }

Adapter:

public interface IPayment { void Pay(decimal amount); } public class PaymentAdapter : IPayment { private readonly OldPaymentGateway _old = new(); public void Pay(decimal amount) { _old.MakePayment((double)amount); } }

πŸ—£ Interview line

“Adapter pattern helps me integrate legacy or third-party systems without modifying existing code.”


5️⃣ Decorator Pattern

πŸ‘‰ Where we use it

  • Adding extra behavior dynamically

  • Logging

  • Caching

  • Authorization checks

πŸ‘‰ Why useful

  • Adds features without modifying original class

  • Follows Open/Closed Principle

πŸ‘‰ Example (Service with logging)

public interface IOrderService { void PlaceOrder(); } public class OrderService : IOrderService { public void PlaceOrder() => Console.WriteLine("Order placed"); }

Decorator:

public class OrderLoggingDecorator : IOrderService { private readonly IOrderService _service; public OrderLoggingDecorator(IOrderService service) { _service = service; } public void PlaceOrder() { Console.WriteLine("Logging before order"); _service.PlaceOrder(); Console.WriteLine("Logging after order"); } }

πŸ—£ Interview line

“Decorator lets me add cross-cutting concerns like logging or caching without touching core logic.”


6️⃣ Observer Pattern

πŸ‘‰ Where we use it

  • Notifications

  • Events

  • Messaging systems

  • Domain events

πŸ‘‰ Why useful

  • One-to-many notification

  • Loose coupling

πŸ‘‰ Example (Order notification)

public interface IObserver { void Notify(string message); }
public class EmailObserver : IObserver { public void Notify(string message) { Console.WriteLine("Email sent: " + message); } }
public class Order { private List<IObserver> observers = new(); public void Subscribe(IObserver observer) { observers.Add(observer); } public void PlaceOrder() { foreach (var o in observers) o.Notify("Order placed"); } }

πŸ—£ Interview line

“Observer is useful for notification systems where multiple services react to one event.”


7️⃣ Strategy Pattern

πŸ‘‰ Where we use it

  • Payment methods

  • Tax calculation

  • Discount logic

  • Sorting algorithms

πŸ‘‰ Why useful

  • Switch behavior at runtime

  • Removes if/else logic

  • Clean & extensible

πŸ‘‰ Example (Payment strategy)

public interface IPaymentStrategy { void Pay(decimal amount); }
public class CardPayment : IPaymentStrategy { public void Pay(decimal amount) => Console.WriteLine("Paid by Card"); } public class UpiPayment : IPaymentStrategy { public void Pay(decimal amount) => Console.WriteLine("Paid by UPI"); }

Context:

public class PaymentService { private readonly IPaymentStrategy _strategy; public PaymentService(IPaymentStrategy strategy) { _strategy = strategy; } public void Process(decimal amount) { _strategy.Pay(amount); } }

πŸ—£ Interview line

“Strategy pattern allows me to choose different algorithms at runtime without changing existing code.”


⭐ One-Screen Interview Summary

PatternWhere used in .NET Core
SingletonLogging, Config, Cache
FactoryObject creation logic
BuilderComplex object creation
AdapterLegacy / 3rd-party integration
DecoratorLogging, caching, security
ObserverEvents, notifications
StrategyPayment, tax, discount logic

🎯 Final Interview Tip

If interviewer asks “Have you used design patterns?”, say:

“Yes, in .NET Core I frequently use Singleton for logging/configuration, Factory for object creation, Strategy for payment and tax logic, Decorator for logging and caching, Adapter for legacy integrations, and Observer for event-based notifications.”

This answer shows real experience, not theory.

Comments

Popular posts from this blog

.NET Core Interview Questions and Answers for 10+ Years Experienced Professionals

What are SOLID Principles?

.NET Core Senior Interview Q&A